package com.mjk.manager.amis.web;

import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.util.ClobUtils;
import com.mjk.common.util.UrlEncoderUtils;
import com.mjk.manager.amis.AmisModelInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

/**
 * amis 打开渲染 打开编辑器 控制器
 */
//@Controller
//@RequestMapping("/amis")
public class AmisController {
	@Autowired
	private BeeDBService beeDBService;
	/**
	 * 渲染界面
	 * @param model
	 * @return
	 */
	@GetMapping("/show")
	public String login(@RequestParam("id")String id, Model model) {
		// 默认登录页面
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new AmisModelInfo());
		wrapper.eq(AmisModelInfo::getId,id);
		AmisModelInfo info =  (AmisModelInfo)beeDBService.findOneByWrapper(wrapper);
		if(null != info && null != info.getInfo()) {
			model.addAttribute("json", ClobUtils.clobToString(info.getInfo()));
		}else{
			model.addAttribute("json","{}");
		}
		model.addAttribute("contextPath","/");
		return "page";
	}

	/**
	 * 打开编辑器
	 * @param model
	 * @return
	 */
	@GetMapping("/editor")
	public String editor(@RequestParam("id")String id,Model model) {
		long time = new Date().getTime();
		model.addAttribute("sysCode", "");
		model.addAttribute("pageId", id);
		model.addAttribute("contextPath", "/");
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new AmisModelInfo());
		wrapper.eq(AmisModelInfo::getId,id);
		AmisModelInfo info =  (AmisModelInfo)beeDBService.findOneByWrapper(wrapper);
		if(null != info && null != info.getInfo()) {
			model.addAttribute("json", ClobUtils.clobToString(info.getInfo()));
		}else{
			model.addAttribute("json","{}");
		}
		return "editor";
	}

    /**
     * 打开主界面
     *
     * @param model
     * @return
     */
    @GetMapping("/app")
    public String app(Model model) {
        model.addAttribute("contextPath", "/");
        return "app";
    }
}
